package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Course;
import cn.sunlelin.educationserver.entity.Search;
import cn.sunlelin.educationserver.mapper.AppraiseMapper;
import cn.sunlelin.educationserver.mapper.CourseMapper;
import cn.sunlelin.educationserver.service.CourseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private AppraiseMapper appraiseMapper;

    @Override
    public void createCourse(Course course) {
        course.setCreateTime(new Date());
        courseMapper.createCourse(course);
    }

    @Override
    public List<Map<String, Object>> findAll(){
        return courseMapper.findAll();
    }

    @Override
    public Map<String, Object> selectCourseById(Integer courseId) {
        return courseMapper.selectCourseById(courseId);
    }

    @Override
    public List<Map<String, Object>> selectCourseByCoursetypeId(Integer coursetypeId) {
        return courseMapper.selectCourseByCoursetypeId(coursetypeId);
    }

    @Override
    public List<Map<String, Object>> selectCourseByFathertypeId(Integer fathertypeId) {
        return courseMapper.selectCourseByFathertypeId(fathertypeId);
    }

    @Override
    public  Page<Map<String,Object>> getPageSearch(Integer currentPage, Integer pageSize, String input,Integer sortModel){
        Search search=new Search(input,sortModel);
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=courseMapper.selectCourseBySearch(search);
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Page<Map<String,Object>> getPageCourse(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=courseMapper.findAll();
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Page<Map<String,Object>> getPageByFathertypeId(Integer currentPage,Integer pageSize,Integer fathertypeId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=courseMapper.selectCourseByFathertypeId(fathertypeId);
        return (Page<Map<String, Object>>) courseList;
    }
    @Override
    public Page<Map<String,Object>> getPageByCoursetypeId(Integer currentPage,Integer pageSize,Integer coursetypeId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=courseMapper.selectCourseByCoursetypeId(coursetypeId);
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Page<Map<String,Object>> getPageByUserId(Integer currentPage,Integer pageSize,Integer userId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=courseMapper.selectCourseByUserId(userId);
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Boolean updateCourse(Course course) {
        if(this.selectCourseById(course.getCourseId())==null)
            return false;
        course.setUpdateTime(new Date());
        courseMapper.updateCourse(course);
        return true;
    }

    @Override
    public Boolean updateScore(Integer courseId){
        if(this.selectCourseById(courseId)==null)
            return false;
        List<Map<String,Object>> appraiseList=appraiseMapper.selectAppraiseByCourseId(courseId);
        float sum=0;
        for (Map<String, Object> stringObjectMap : appraiseList) {
            sum += (Integer) stringObjectMap.get("score");
        }
        sum/=appraiseList.size();
        Course course=new Course();
        course.setCourseId(courseId);
        course.setScore(sum);
        course.setUpdateTime(new Date());
        courseMapper.updateScore(course);
        return true;
    }

    @Override
    public Boolean addAttendNum(Integer courseId) {
        if(this.selectCourseById(courseId)==null)
            return false;
        Course course=new Course();
        course.setCourseId(courseId);
        course.setUpdateTime(new Date());
        courseMapper.addAttendNum(course);
        return true;
    }

    @Override
    public Boolean deleteCourse(Integer courseId) {
        if(this.selectCourseById(courseId)==null)
            return false;
        Course course=new Course();
        course.setCourseId(courseId);
        course.setUpdateTime(new Date());
        courseMapper.deleteCourse(course);
        return true;
    }
}
